package cn.niit.letuseatdemo.controller.user;


import cn.niit.letuseatdemo.entity.domain.Classify;
import cn.niit.letuseatdemo.entity.domain.Goods;
import cn.niit.letuseatdemo.entity.domain.Store;

import cn.niit.letuseatdemo.entity.vo.response.BaseResponse;
import cn.niit.letuseatdemo.service.ClassifyService;
import cn.niit.letuseatdemo.service.GoodsService;
import cn.niit.letuseatdemo.service.StoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 用户查看菜品之类的操作
 * @Author Zhang Shiqi
 * @CreateTime 2022/11/3 0:15
 */
@Slf4j
@RequestMapping("/api/store")
@RestController
public class UserGoodController {

    //查菜
    @Autowired
    private GoodsService goodsService;

    //查商家
    @Autowired
    private StoreService storeService;

    //查类别
    @Autowired
    private ClassifyService classifyService;

    /*
    * @Description: 查商家列表
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/12/7 18:43
     * @param: store
     * @return: cn.niit.letuseatdemo.entity.vo.response.BaseResponse
    **/
    @GetMapping()
    public BaseResponse storeList(Store store){
        List<Store> stores =storeService.selectAllStoreList(store);
        Map<String, Object> map = new HashMap<>();
        map.put("stores", stores);
        return BaseResponse.success(map);
    }


    /*
    * @Description: 根据商家id获取商家信息
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/12/8 3:24
     * @param: storeId
     * @return: cn.niit.letuseatdemo.entity.vo.response.BaseResponse
    **/
    @GetMapping("/{storeId}")
    public BaseResponse storeMes(@PathVariable("storeId")Long storeId){
        Store store = storeService.selectAdminStoreByStoreId(storeId);
        return BaseResponse.success(store);
    }


    /*
     * @Description: 根据商家id获取菜品信息
     * @Author: Zhang Shiqi
     * @CreateTime： 2022/12/8 3:24
     * @param: storeId
     * @return: cn.niit.letuseatdemo.entity.vo.response.BaseResponse
     **/
    @GetMapping("/goods/{storeId}")
    public BaseResponse goodsList(Goods good,@PathVariable("storeId")Long storeId){
        List<Goods> goods =goodsService.selectGoodsByStoreId(good,storeId);
        Map<String, Object> map = new HashMap<>();
        map.put("goods", goods);
        return BaseResponse.success(map);

    }

    /*
    * @Description: 详细菜品显示
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/12/8 4:42
     * @param: storeId
     * @param: goodsId 
     * @return: cn.niit.letuseatdemo.entity.vo.response.BaseResponse 
    **/
    @GetMapping("/goods/{storeId}/{goodsId}")
    public BaseResponse goodsMes(@PathVariable("storeId")Long storeId,@PathVariable("goodsId")Long goodsId){
        Goods goods = goodsService.selectGoodsByStoreIdAndGoodsId(storeId,goodsId);
        return BaseResponse.success(goods);
    }

    /*
    * @Description: 用户侧： 根据店铺ID查询菜品类别
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/12/8 10:21
     * @return: cn.niit.letuseatdemo.entity.vo.response.BaseResponse
    **/
    @GetMapping("/goods/classify/{storeId}")
    public BaseResponse goodsClassify(@PathVariable("storeId") Long storeId){
        List<Classify> goods = classifyService.getClassifyByStoreId(storeId);
        return BaseResponse.success(goods);
    }

    /*
    * @Description: 根据菜品类别查菜品列表
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/12/8 10:37
     * @param: good
     * @param: storeId
     * @param: goodsClassify
     * @return: cn.niit.letuseatdemo.entity.vo.response.BaseResponse
    **/
    @GetMapping("/goods/Classify/{storeId}/{goodsClassify}")
    public BaseResponse goodsTypelist(Goods good,@PathVariable("storeId")Long storeId,@PathVariable("goodsClassify")String goodsClassify){
        List<Goods> goods=goodsService.selectGoodsByGoodsClassify(good,storeId,goodsClassify);
        Map<String, Object> map = new HashMap<>();
        map.put("goods", goods);
        return BaseResponse.success(map);
    }

    /*
    * @Description: 显示商品列表
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/12/7 18:08
     * @param: patient
     * @return: cn.niit.letuseatdemo.entity.vo.response.BaseResponse
    **/
    //@GetMapping()
    public BaseResponse goodslist(Goods goods){
//        List<Goods> patients = GoodsService.selectPatientList(patient);
//        Map<String, Object> map = new HashMap<>();
//        map.put("patients", patients);

//        return BaseResponse.success(map);
        return null;
    }

    /*
    * @Description: 搜索菜品、菜品显示
    * 改为： 搜索菜品/商家/全部
    * 1.判断搜索类型
    * 2.调用不同的方法实现
    * 3.返回一个列表，相应的商品列表
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/11/3 0:19
     * @param: model
     * @param: goods
     * @return: java.lang.String
    **/
    //@GetMapping("/usergoods/{value}")
    public String index(Model model, Goods goods,@PathVariable("value") Integer value){
        return "/user/usergoods";
    }
    /*
    * @Description: 搜索商家、商家显示
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/11/3 0:41
     * @param: model
     * @param: storeInfo
     * @return: java.lang.String
    **/
    //@GetMapping("/userstores")
//    public String index_1(Model model, StoreInfo storeInfo){
////        model.addAttribute("storeInfo", storeInfo);
////        List<StoreInfo> storeInfoList = storeInfoService.selectStoreInfoListSearch(storeInfo);
////        PageInfo<StoreInfo> pageInfo = new PageInfo<>(storeInfoList);
////        log.info("分页对象 = {}", pageInfo);
////
////        log.info("查询出来的商家列表：{}", storeInfoList);
////
////        model.addAttribute("goodsList", storeInfoList);
////        model.addAttribute("pageInfo", pageInfo);
//        return "/user/userstores";
//    }

    /*
    * @Description: 查看菜品详情
    * 点击详情进入当个商品的详情的页面
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/11/8 1:24
     * @param: model
     * @param: goods 
     * @return: java.lang.String 
    **/
    //@GetMapping("/usergoods/{goodsId}")
    public String goosdetail(Model model,@PathVariable("goodsId") Long goodsId){
        log.info("要查看菜品的id："+goodsId);
        if (null != goodsId && goodsId >0){
            Goods goods=goodsService.selectGoodsByGoodsId(goodsId);
            model.addAttribute("goods",goods);

            log.info("查询出来的菜品：{}", goods);

        }

        return "user/goodsdetail";
    }


    /*
     * @Description: 根据类别id查看商家列表
     * @Author: 陆品贤
     * @CreateTime： 2022/11/8 1:24
     * @param:categoryId
     * @return:cn.niit.letuseatdemo.entity.vo.response.BaseResponse
     **/
    @GetMapping("/categoryshop/{categoryId}")
    public BaseResponse selectBycategory(@PathVariable("categoryId") Long categoryId) {
        List<Store> store = storeService.selectStoreListByCategoryId(categoryId);
        //api使用basereponse返回给前端
        return BaseResponse.success(store);
    }


}
